import apiClient from './index';

// 获取学生排名参数
export interface StudentRankingParams {
  class_id: number;
}

// 学生排名项类型
export interface StudentRankingItem {
  student_number: string;
  student_name: string;
  performance_points: number;
  rank: number;
  user_id: number;
  student_id: number;
  class_id: number;
}

// 学生排名响应
export interface StudentRankingResponse {
  code: number;
  content: {
    class_id: number;
    class_name: string;
    ranking_list: StudentRankingItem[]; // 学生列表在ranking_list字段中
    total_students: number;
  };
  msg: string;
}

// 创建学生参数
export interface CreateStudentParams {
  class_id: number;
  student_id: string; // 学号
  name: string;
  teacher_password: string;
}

// 创建学生响应
export interface CreateStudentResponse {
  code: number;
  content: {
    class_id: number;
    password: string;
    student_id: string;
    user_id: number;
    username: string;
  };
  msg: string;
}

// 更新学生参数
export interface UpdateStudentParams {
  user_id: number;
  student_id?: string;
  name?: string;
  class_id?: number;
  password?: string; // 可选，不修改则不传
  confirm_password?: string; // 可选
  admin_password: string;
}

// 更新学生响应
export interface UpdateStudentResponse {
  code: number;
  msg: string;
}

// 获取无小组学生参数
export interface ListNoGroupStudentsParams {
  class_id: number;
  has_group: 0;
  name?: string;
}

// 获取全体学生参数
export interface ListStudentsParams {
  class_id?: number;
  name?: string;
}

// 无小组学生项
export interface NoGroupStudentItem {
  student_id: number;
  student_number: string;
  name: string;
}

// 学生项
export interface StudentItem {
  student_id: number;
  student_number: string;
  name: string;
  grade: number;
  class_name: string;
}

// 无小组学生响应
export interface ListNoGroupStudentsResponse {
  code: number;
  content: NoGroupStudentItem[];
  msg: string;
}

// 学生列表响应
export interface ListStudentsResponse {
  code: number;
  content: StudentItem[];
  msg: string;
}

/**
 * 获取学生积分排名
 */
export const getStudentRanking = async (
  params: StudentRankingParams
): Promise<StudentRankingResponse> => {
  const response = await apiClient.post<StudentRankingResponse>('/point/ranking', params);
  return response.data;
};

/**
 * 创建学生账号
 */
export const createStudent = async (
  params: CreateStudentParams
): Promise<CreateStudentResponse> => {
  const response = await apiClient.post<CreateStudentResponse>('/register/student', params);
  return response.data;
};

/**
 * 更新学生信息
 */
export const updateStudent = async (
  params: UpdateStudentParams
): Promise<UpdateStudentResponse> => {
  const response = await apiClient.post<UpdateStudentResponse>('/studentmgr/update-student', params);
  return response.data;
};

/**
 * 获取无小组学生列表
 */
export const listNoGroupStudents = async (
  params: ListNoGroupStudentsParams
): Promise<ListNoGroupStudentsResponse> => {
  const response = await apiClient.post<ListNoGroupStudentsResponse>('/studentmgr/list-students', params);
  return response.data;
};

/**
 * 获取学生列表
 */
export const listStudents = async (
  params: ListStudentsParams
): Promise<ListStudentsResponse> => {
  const response = await apiClient.post<ListStudentsResponse>('/studentmgr/list-students', params);
  return response.data;
};